import 'package:custom_pop_up_menu/custom_pop_up_menu.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
import 'package:get/get.dart';
import 'package:mziot/app/config/design.dart';
import 'package:mziot/app/data/scene_list_entity.dart';
import 'package:mziot/app/modules/scene/scene_provider.dart';
import 'package:mziot/app/routes/app_pages.dart';
import 'package:mziot/app/tools/blue_service.dart';
import 'package:mziot/app/tools/tools.dart';
import 'package:mziot/generated/locales.g.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class SceneController extends GetxController {
  TextEditingController nameController = TextEditingController();
  SceneProvider provider = SceneProvider();
  final sceneList = <SceneListEntity>[].obs;
  RefreshController refreshController = RefreshController();
  CustomPopupMenuController menuController = CustomPopupMenuController();

  @override
  void onInit() {
    super.onInit();

    fetchSceneList();
  }

  @override
  void onReady() {
    super.onReady();
  }

  void fetchSceneList() {
    if (sceneList.isEmpty) {
      EasyLoading.show();
    }
    provider.takeSceneList().then((value) {
      if (value.isSuccess) {
        sceneList.value = value.data ?? [];
        sceneList.map((element) {
          element.turnState.value = element.sceneSwitch ?? false;
          element.lockState.value = element.isLock ?? false;
          return element;
        }).toList();
      }
      refreshController.refreshCompleted();
    }).whenComplete(() {
      EasyLoading.dismiss();
    });
  }

  void fetchTurnScene(int index) {
    EasyLoading.show();
    final sceneId = sceneList[index].sceneId ?? 0;
    final turn = sceneList[index].turnState.value;
    provider.turnScene(sceneId, !turn).then((value) {
      if (value.isSuccess) {
        sceneList[index].turnState.value = !turn;
        BlueService.share.execCmds(value.data);
      }
    }).whenComplete(() {
      EasyLoading.dismiss();
    });
  }

  void fetchLockScene(int index) {
    EasyLoading.show();
    final sceneId = sceneList[index].sceneId ?? 0;
    final lock = sceneList[index].lockState.value;
    provider.lockScene(sceneId, !lock).then((value) {
      if (value.isSuccess) {
        sceneList[index].lockState.value = !lock;
        BlueService.share.execCmds(value.data);
      }
    }).whenComplete(() {
      EasyLoading.dismiss();
    });
  }

  //删除确认
  void deleteConfirm(int sceneId) {
    var message = LocaleKeys.scene_delete_title.tr;
    if (BlueService.share.deviceState != DeviceConnectionState.connected) {
      message = LocaleKeys.scene_delete_alert.tr;
    }
    Get.generalDialog(pageBuilder: (ctx, a, s) {
      return CupertinoAlertDialog(
        content: Text(
          message,
          style: MFont.semi_Bold17.apply(color: MColor.xFF333333),
        ),
        actions: <Widget>[
          TextButton(
            onPressed: () {
              Get.back();
            },
            child: Text(
              LocaleKeys.home_no.tr,
              style: MFont.regular17.apply(color: MColor.xFF333333),
            ),
          ),
          TextButton(
            onPressed: () {
              Get.back();
              fetchDelete(sceneId);
            },
            child: Text(
              LocaleKeys.home_yes.tr,
              style: MFont.semi_Bold17.apply(color: MColor.xFF333333),
            ),
          ),
        ],
      );
    });
  }

  void fetchDelete(int sceneId) {
    EasyLoading.show();
    provider.deleteScene(sceneId).then((value) {
      if (value.isSuccess) {
        List<SceneListEntity> list = sceneList.value;
        list.removeWhere((element) => element.sceneId == sceneId);
        sceneList.value = [];
        sceneList.value = list;
        BlueService.share.execCmds(value.data);
      }
      showToast(value.msg ?? '');
    }).whenComplete(() {
      EasyLoading.dismiss();
    });
  }

  void toDetail(int index) {
    final id = sceneList[index].sceneId;
    Get.toNamed(Routes.SCENE_DETAIL, arguments: id)?.then((value) {
      if (value != null) {
        fetchSceneList();
      }
    });
  }

  @override
  void onClose() {
    refreshController.dispose();
    menuController.dispose();
  }
}
